<template>
  <div class="app-container">
    <div>
      <!-- <avue-form
        :option="option"
        v-model="queryParams"
        ref="searchForm"
        v-show="showSearch"
        @keydown.enter="getList"
      >
        <template #menu-form>
          <el-button icon="Search" type="primary" @click="getList" class="m-l-3"
            >查询</el-button
          >
          <el-button icon="Refresh" @click="handleEmpty(searchForm)"
            >重置</el-button
          >
        </template>
      </avue-form> -->
      <!-- <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="EditPen"
            @click="handleUpdate"
            :disabled="disUpdate"
            >编辑申请</el-button
          >
        </el-col>
      </el-row> -->

      <el-table
        v-loading="loading"
        :data="tableList"
        row-key="id"
        stripe
        row-class-name="cursor-pointer"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column prop="fileName" label="文件名" min-width="130" />
        <el-table-column prop="createTime" label="填表时间" width="130">
          <template #default="scope">
            <div>
              {{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="approvalStatus" label="状态" width="130" />
        <el-table-column prop="createName" label="填报人" width="130" />
        <el-table-column prop="approvalTime" label="审核日期" width="130">
          <template #default="scope">
            <div>
              {{ parseTime(scope.row.approvalTime, '{y}-{m}-{d}') }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="status" label="状态" width="130">
          <template #default="scope">
            <div>
              <dict-drop :options="order_status" :value="scope.row.status" />
              <span
                class="text-12px text-#999999"
                v-if="scope.row.approvalStatus"
                >({{ scope.row.approvalStatus }})</span
              >
            </div>
          </template>
        </el-table-column> -->
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding"
          min-width="120"
        >
          <template #default="scope">
            <el-button
              v-if="scope.row.approvalStatus != '流程未发起'"
              plain
              type="primary"
              size="small"
              @click="handleProcess(scope.row)"
              >查看流程</el-button
            >
            <el-button
              v-if="
                scope.row.approvalStatus == '流程未发起' ||
                scope.row.approvalStatus == '已取消'
              "
              plain
              type="primary"
              size="small"
              class="pl-6px"
              :disabled="scope.row.createBy != userStore.userInfo.userName"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="
                scope.row.approvalStatus != '流程未发起' &&
                scope.row.approvalStatus != '已取消' &&
                scope.row.approvalStatus != '审批通过'
              "
              :disabled="scope.row.createBy != userStore.userInfo.userName"
              plain
              type="primary"
              size="small"
              class="pl-6px"
              @click="cancelProcess(scope.row)"
              >取消申请</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 实施记录表单弹窗 -->
    <el-dialog
      v-if="recordFormDialog.open"
      :title="recordFormDialog.title"
      v-model="recordFormDialog.open"
      width="80%"
      :close-on-click-modal="false"
    >
      <SituationForm
        :situationId="situationId"
        @processFun="processFun"
        @saveFun="saveFun"
      />
    </el-dialog>
  </div>
</template>
<script setup name="SituationTable">
import { useRouter } from 'vue-router'
import { listSituation } from '@/api/projectManage/project'
import {
  companyOption //归属公司
} from '@/hooks/dict/order'
import useUserStore from '@/store/modules/user'
import { stopProcess } from '@/api/workflow/process'
import SituationForm from '@/views/salesManage/projectManage/ImplementRecords/situationForm'
const userStore = useUserStore()
const router = useRouter()
const { proxy } = getCurrentInstance()

const { yes_or_no, sys_have_no, supplier_type } = proxy.useDict(
  'yes_or_no', //是否
  'sys_have_no', //有无
  'supplier_type'
)

const props = defineProps({
  queryParams: {
    type: [Object],
    require: true
  }
})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: ''
  },
  ids: [],
  selectArr: [],
  total: 0,
  disUpdate: true /* 编辑按钮是否可编辑 */,
  disDelete: true /* 关闭按钮是否可编辑 */,
  loading: false,
  tableList: []
})
const {
  queryParams,
  ids,
  selectArr,
  total,
  disUpdate,
  disDelete,
  loading,
  tableList
} = toRefs(data)

watch(
  () => props.queryParams,
  () => {
    queryParams.value.projectId = props.queryParams.projectId
    getList()
  },
  {
    immediate: true,
    deep: true
  }
)

// 初始化
async function init() {}
// 重置
function handleEmpty(formEl) {
  proxy.resetForm('searchForm')
  proxy.resetForm('seniorSearchForm')
  // proxy.$refs.searchForm.resetForm()
  getList()
}

// 获取数据列表
function getList() {
  loading.value = true
  listSituation(queryParams.value).then(res => {
    loading.value = false
    tableList.value = res.rows
    total.value = res.total
  })
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  selectArr.value = selection
  let approvalUpdate = selectArr.value.some(
    item => item.approvalStatus == '流程未发起'
  )
  // let approvalStatus = selectArr.value.some(
  //   item => item.approvalStatus == '审批通过'
  // )
  disUpdate.value = selection.length != 1 || !approvalUpdate
  disDelete.value = !selection.length
}
function goTo(url, data) {
  router.push({ path: url, query: data })
}

// 查看流程
const handleProcess = row => {
  router.push({
    path: '/process/workDetail',
    query: {
      procInsId: row.proInsId,
      //   taskId: row.taskId,
      processed: false
    }
  })
}
// 取消申请
const cancelProcess = row => {
  stopProcess({ procInsId: row.proInsId }).then(res => {
    proxy.$message.success('取消申请成功')
    getList()
  })
}

const recordFormDialog = ref({
  title: '选择新增实施记录类型',
  open: false
})
const situationId = ref('')
// 编辑
const handleEdit = row => {
  situationId.value = row.id
  recordFormDialog.value.open = true
}
const processFun = val => {
  console.log('发起申请==》', val)
  recordFormDialog.value.open = false
  getList()
}
const saveFun = val => {
  console.log('保存==》', val)
}

init()
</script>
<style scoped>
.search-drawer-retract {
  color: #999999;
}
</style>
